﻿using OpenTK.Input;
using OpenTK;
using System;

namespace Painter3D
{
    static class InputManager
    {
        static public float timeStep
        {
            get; private set;
        }
        static public KeyboardState keyboard
        {
            get; private set;
        }
        static public MouseState mouse
        {
            get; private set;
        }

        /// <summary>
        /// 返回标准化后的鼠标坐标，OPENGL屏幕坐标
        /// </summary>
        /// <param name="mouseState">鼠标状态</param>
        /// <returns>标准化坐标</returns>
        static public Vector2 NormalizedPosition(this MouseState mouseState)
        {
            return new Vector2((float)mouseState.X / Window.windowConfig.width * 2 - 1, -((float)mouseState.Y / Window.windowConfig.height * 2 - 1));
        }

        static public void OperateInput(float step)
        {
            timeStep = step;
            keyboard = Keyboard.GetState();
        }

        static public void Init(Window window)
        {
            Mouse.SetPosition(Window.windowConfig.window.Bounds.Left + Window.windowConfig.width / 2, Window.windowConfig.window.Bounds.Top + Window.windowConfig.height / 2);
            window.MouseMove += (sender, e) =>
            {
                mouse = e.Mouse;
            };
        }

        /// <summary>
        /// 上升沿触发器
        /// </summary>
        public class UpTriger
        {
            private bool state = false;

            public bool Sign(bool input)
            {
                bool output = !state && input;
                if (!input && state)
                    state = false;
                if (input && !state)
                    state = true;
                return output;
            }

        }
    }
}
